﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace VkSample84
{
    [ModulePriority(ModulePriority.Assets)]
    public class OBJAssetsModule : VulkanModule, IVulkanRenderWapperModule
    {
        private readonly ILogger<OBJAssetsModule> _Logger;

        public OBJAssetsModule(ILogger<OBJAssetsModule> logger)
        {
            _Logger = logger;
        }

        public VulkanRenderWapper RenderWapper { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            try
            {
                var basePath = AppContext.BaseDirectory;
                var modelsPath = Path.Combine(basePath, "Assets", "Models");
                graphicsManager.TryGetRenderGroup("OBJGroup", out IRenderGroup group);
                var lDevice = graphicsManager.VulkanDevice;
                var archetype = ContextManager.CreateArchetype(new TransformComponent(), new OBJComponent());
                var pmArchetype = ContextManager.CreateArchetype(new TransformComponent(), new PMComponent());
                var lightArchetype = ContextManager.CreateArchetype(new LightComponent());

                ISEADocument document = new ISEADocument();
                await document.Load(Path.Combine(AppContext.BaseDirectory, "test84.isea"));
                var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
                var light = graphicsManager.CreatePointLight<RenderObject>("main", new Vector3(100, 100, 100), new LightStrength(0.7f, 0.7f, 0.7f, 1.0f));

                foreach(var item in renders)
                {
                    if(item.Name == "PM")
                    {
                        var pmGroup = new PMGroup
                        {
                            Transform = new TransformComponent { Scale = new Vector3(8, 8, 8), Angle = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), -90 * MathF.PI / 180.0f), Position = new Vector3(0, 0, 0) },
                            PMComponent = new PMComponent() { Scale2 = new Vector3(2.0f, 1.0f, 1.7f) }
                        };
                        ContextManager.CreateContexts(1, pmArchetype, group, item, pmGroup);
                    }
                    else
                    {
                        ContextManager.CreateContexts(1, archetype, group, item);
                    }
                }
                ContextManager.CreateContexts(1, lightArchetype, group, light);
            }
            catch (Exception e)
            {
                _Logger.LogError(e.Message + "\r\n" + e.StackTrace);
            }
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
